<?php
	$module = array('id' => intval($_REQUEST['modules_id']));
	
	if (local_post()) {
		$errors = array();
		
		if (!empty($errors)) errors($errors);
		else {
			$module['name'] = db_escape_string($_POST['name']);
			$module['folder'] = db_escape_string($_POST['folder']);
			$module['active'] = db_escape_string($_POST['active']);
			
			$module = db_save($module, 'modules');		
			
			if ($_POST['button'] == 'Save and Close') location('/modules/admin/index');
			elseif ($_POST['button'] == 'Save and New') location('/modules/admin/edit');
		}
	}
	
	$module = db_select($module['id'], 'modules');
?>

<div class="warning">
	<strong>Warning</strong>: Changing the folder of a module will likely break your website!
</div>

<form action="?modules_id=<?php echo $module['id']?>" method="post">
	<fieldset>
		<legend>Module</legend>
		
		<label for="name">
			Name
			<input type="text" name="name" id="name" value="<?php echo $module['name']?>" />
		</label>
		
		<label for="folder">
			Folder
			<select name="folder" id="folder">
				<option value=""> - Select One - </option>
			<?php
				foreach (scandir(PHPDRIVER . 'modules/') as $folder) {
					if (substr($folder, 0, 1) == '.' || !is_dir(PHPDRIVER . "modules/$folder")) continue;
					$selected = ($folder == $module['folder']) ? ' selected="selected"' : ''; 
			?>
				<option value="<?php echo $folder?>"<?php echo $selected?>><?php echo $folder?></option>
			<?php
				}
			?>
			</select>
		</label>
		
		<label for="active">
			<input type="checkbox" name="active" id="acitve" value="1" <?php if ($module['active'] == 1) echo 'checked="checked" '; ?>/>
			Active
		</label>
	</fieldset>
	
	<?php save_buttons(); ?>
</form>